<?php
namespace App\Libs\Common;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Libs\Vendor\JdPaySdk\AgreementSign;
use App\Libs\Vendor\JdPaySdk\TradeWithAgreement;

/**
 *   京东签约代扣
 */
class JdDkController extends Controller
{	
	

    public function  __construct()
    {   
        header("Content-type: text/html; charset=utf-8");
    }


    /**
     * 代扣签约
     * @return [phone]    [用户手机号]
     * @return [order_id] [订单ID用于查询协议]
     */
    public function contract($phone,$order_id)
    {   
        $token=DB::table("user_login")->where("phone",$phone)->value("token");
        $return_url="https://n.lvyuecp.com/wapJd/panel.html?v=052801&phone=".$phone."&token=".$token;
        $notify_url="https://n.lvyuecp.com/OutApi/JdDkYb/signNotify";
        $obj = new AgreementSign();

        $url=$obj->getAgreementSignUrl($phone,$order_id,$return_url,$notify_url);

        return $url;
    }


    /**
     *    协议查询
     */
    public function contractQuery()
    {
        $order_id='2018053103105198509748';
        $agreement_no='2018053130183496882';
        $obj = new AgreementSign();
        $res=$obj->agreementQuery($order_id,$agreement_no);

        print_r($res);
    }



    /**
     *   协议取消
     *   @param  order_id     交易号分期表ID
     *   @param agreement_no  代扣协议号
     */
    public function contractCancel($order_id,$agreement_no)
    {
        $obj = new AgreementSign();
        $res=$obj->agreementCancel($order_id,$agreement_no);

       if ($res ===true) 
       {    
           return true;
       }else{
           return false;
       }
    }



    /**
     *  根据签约协议代扣
     *  @param out_trade_no  交易号分期表ID
     *  @param agreement_no  代扣协议号
     *  @param money         金额
     */
    public function cut_payment($out_trade_no,$agreement_no,$money)
    {
        $notify_url="https://n.lvyuecp.com/OutApi/JdDkYb/tradeNotify";
        $obj = new TradeWithAgreement();
        $result=$obj->agreementPay($out_trade_no,$agreement_no,$money,$notify_url);
        return $result;
    }

		
	
}